#include "cxshowthread.h"
#include <fb.h>
#include <fcntl.h>
#include <sys/ioctl.h>

CxShowThread::CxShowThread(int display_fd, CxImageBuffer<4> *pbuffer, uchar *framebuffer, QObject *parent):
    m_framebuffer(framebuffer),
    m_pbuffer(pbuffer),m_currentcount(0),
    m_displayfd(display_fd),
    QThread(parent)
{
}
CxShowThread::~CxShowThread()
{
}

void    CxShowThread::run()
{
    _imageData data;

    const uint                  imagesize=1024*768*4;
    struct fb_var_screeninfo    varinfo;
    struct fb_fix_screeninfo    fixinfo;
    ioctl(m_displayfd, FBIOGET_FSCREENINFO, &fixinfo);

    QTime time;

    while(1)
    {       
        data=m_pbuffer->pop_image();

        if(m_currentcount==0)
        {
            for(int i=0;i<data.height();++i)
            {
                memcpy(m_framebuffer+fixinfo.line_length*i,data.data()+i*(data.width()<<2),data.width()<<2);
            }
        }
        else
        {
            for(int i=0;i<data.height();++i)
            {
                memcpy(m_framebuffer+imagesize+fixinfo.line_length*i,data.data()+i*(data.width()<<2),data.width()<<2);
            }
        }

        ioctl(m_displayfd, FBIOGET_VSCREENINFO, &varinfo);
        varinfo.yoffset= m_currentcount ? imagesize : 0;
        ioctl(m_displayfd, FBIOPUT_VSCREENINFO, &varinfo);

        m_currentcount =  m_currentcount ? 0 : 1;

        msleep(2);
    }
}
